<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>菜单管理管理</title>
<meta name="decorator" content="default"/>
<script type="text/javascript">
	//url
	var rootUrl = '${ctx}/sys/menu/';
	var listUrl = rootUrl+'listMenu';
	var cardUrl = rootUrl+'formMenu';
	var sortUrl = rootUrl+'updateMenuSort';
	var statusUrl = rootUrl+"ajaxUpdateStatus";
	
	$(document).ready(function() {
		jQuery('[name="iconIcon"]').each(function(){
			var className= this.className;
			if(className == null || className == '' || className.indexOf('icon-')>-1){
				return ;
			}
			className = 'icon-'+jQuery.trim(className);
			this.className = className;
		});
	});
	function page(n, s) {
		$("#pageNo").val(n);
		$("#pageSize").val(s);
		$("#searchForm").submit();
		return false;
	}
	
	function add() { //新增记录
		var url = cardUrl + '?opt=add';
		url = addUrlParam(url,'parent.id',jQuery('[name="parent.id"]').val());
		parent.freshTabs(jQuery(window.parent.document).find('[link="iframeContent2"]'), url);
	}
	
	function edit(obj, isEdit) { //修改记录
		var curentTr = null;
		if (obj != null) {
			curentTr = jQuery(obj).parents('tr')[0];
		} else {
			var table = jQuery("#contentTable");
			var checkboxs = jQuery('input[attrname="sort"]:checked', table);
			if (checkboxs.length == 0) {
				alertx("请选择一行数据！");return;
			} else if (checkboxs.length > 1) {
				alertx("只能选择一行数据！");return;
			}
			curentTr = jQuery(checkboxs[0]).parents('tr')[0];
		}
		var id = jQuery('[attrname="id"]', curentTr).val();
		if (id != null || id != '') {
			var opt = (isEdit==true || isEdit == null) ? 'edit' : 'other';
			var url = cardUrl + '?opt=' + opt + '&id=' + id;
			parent.freshTabs(jQuery(window.parent.document).find('[link="iframeContent2"]'), url);
		}
	}
	
	function detail(obj) { //查看记录详情
		edit(obj, false);
	}
	
	function del(obj) { //删除记录
		if (obj != null) { //单个删除
			var curentTr = jQuery(obj).parents('tr')[0];
			var id = jQuery('[attrname="id"]', curentTr).val();
			var url = rootUrl + 'delete?id=' + id;
			confirmx('确认要删除该赛事视频吗？', function() {
				ajaxDelete(id);
			});
		} else { //批量删除
			var table = jQuery("#contentTable");
			var checkboxs = jQuery('input[attrname="sort"]:checked', table);
			if (checkboxs.length == 0) {
				alertx("请选择至少一行数据！");
				return;
			}
			var ids = [];
			for (var i = 0; i < checkboxs.length; i++) {
				var curentTr = jQuery(checkboxs[i]).parents('tr')[0];
				var id = jQuery('[attrname="id"]', curentTr).val();
				ids.push(id);
			}
			var url = rootUrl + 'batchDelete?ids=' + ids.join();
			confirmx('确认要删除所选赛事视频吗？', function() {
				ajaxDelete(ids.join());
			});
		}
		parent.refreshTree();
	}
	function ajaxDelete(ids){
		var url = rootUrl + 'ajaxDelete?rk='+Math.random();
		var data = {ids:ids};
	    jQuery.ajax({  
			url: url, async : false,type: "POST",
			data : data,
			success: function(response) {
				info = response;
				if(info.success == '1'){
					parent.refreshTree();
					queryList();
				}
				alertx(info.message);
			},
			error: function(e){console.error(e);}
		});
	}
	function submitData(){ //提交表单
		var formObj = null;
		if(obj == null){
			formObj = jQuery("#searchForm")[0];
		}else{
			formObj = jQuery(obj).parents('form')[0];
		}
		if(formObj!=null){
			formObj.submit();
		}
	}
	
	function resetForm(obj) { //重置查询条件
		var formObj = null;
		if(obj == null){
			formObj = jQuery("#searchForm")[0];
		}else{
			formObj = jQuery(obj).parents('form')[0];
		}
		if (jQuery(formObj)[0] != null) {
			jQuery(formObj)[0].reset();
		}
	}
	
	function clearForm(obj){ //清空查询条件
		var formObj = null;
		if(obj == null){
			formObj = jQuery("#searchForm")[0];
		}else{
			formObj = jQuery(obj).parents('form')[0];
		}
		if (jQuery(formObj)[0] != null) {
			jQuery(formObj).find(':input').each(function(){ 
	            switch(this.type){  
	                case 'passsword':  
	                case 'select-multiple':  
	                case 'select-one':  
	                case 'text': 
	                case 'hidden': 
	                case 'textarea':  
	                    jQuery(this).val('');   //jQuery('.select2-chosen',formObj).html('');
	                    break;  
	                case 'checkbox':  
	                case 'radio':  
	                    this.checked = false;  
	            }  
	            if(this.type.indexOf('select')>-1){
	            	jQuery(this).select2();   //jQuery(this).select2();
	            }
	        });     
		}
	}
	
	function queryList(){
		var form = jQuery('#searchForm')[0];
		form.submit();
	}
	
	function updateSort() {
	  loading('正在提交，请稍等...');
	  var url = $("#listForm").attr("action");
	  var sortUrls = addUrlParam(sortUrl,'parent.id',jQuery('[name="parent.id"]').val());
	  $("#listForm").attr("action",sortUrls );
      $("#listForm").submit();
      $("#listForm").attr("action",url );
	}
	
	function ajaxUpateStatus(id,status){
		var url = statusUrl;
		var data = {id:id,status:status};
	    jQuery.ajax({  
			url: url, async : false,type: "POST",
			data : data,
			success: function(response) {
				info = response;
				if(info.success == '1'){
					queryList();
				}
				alertx(info.message);
			},
			error: function(e){console.error(e);}
		});
	}
	
</script>
<style type="text/css">
.tableHead th, td {
	text-align: center;
}
</style>
</head>
<body>
	
	<form:form id="searchForm" modelAttribute="menu" action="${ctx}/sys/menu/listMenu" method="post" class="breadcrumb form-search">
		<input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
		<input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
		<input id="refer.type" name="refer.type" type="hidden" value="${menu.refer.type}"/>
		<input id="refer.map" name="refer.map" type="hidden" value="${menu.refer.map}"/>
		<input id="refer.dialog" name="refer.dialog" type="hidden" value="${menu.refer.dialog}"/>
		
		<ul class="ul-form">
			<li><label>名称：</label>
				<form:input path="name" htmlEscape="false" maxlength="100" class="input-medium"/>
			</li>
			<li><label class="control-label">上级菜单:</label>
	            <sys:treeselect id="menu" name="parent.id" value="${menu.parent.id}" 
	            labelName="parent.name" labelValue="${fns:getMenu(menu.parent.id).name}"
				title="菜单" url="/sys/menu/treeData" extId="${menu.id}" cssClass="required"/>
			</li>
			<!--<li><label>排序字段：</label> 
				<form:radiobuttons  path="orderBy" items="${fns:getDictList('recommen_sort')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
			</li>
			<li><label>排序方式：</label>
				<form:radiobuttons  path="sortType" items="${fns:getDictList('recommen_sort_type')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
			</li>-->
			<li class="clearfix"></li> 
		</ul>
		<br>
		<div style="text-align: right;display: block;">
				<input id="btnSubmit" class="btn btn-primary" type="submit" value="查询"/>
				<input id="btnReset" class="btn " type="reset" value="重置" onclick="resetForm(this)"/>
				<input id="btnClear" class="btn " type="button" value="清除" onclick="clearForm(this)"/>
		</div>
	</form:form>
	<sys:message content="${message}"/>
	<c:if test='${menu.refer.type != 1}'>
	<div class="" style="margin-bottom: 3px;">
		<shiro:hasPermission name="sys:menu:edit">&nbsp;
			<input id="btnAdd" class="btn btn-primary" type="button" value="新 增" onclick="add()"/>&nbsp;
			<input id="btnEdit" class="btn btn-primary" type="button" value="修 改" onclick="edit()"/>&nbsp;
			<input id="btnDel" class="btn btn-primary" type="button" value="删 除" onclick="del()"/>&nbsp;
			<input id="btnSort" class="btn " type="button" value="保存排序" onclick="updateSort()"/>
		</shiro:hasPermission>
		<shiro:hasPermission name="sys:menu:view">
			<input id="btnDetail" class="btn " type="button" value="查 看" onclick="detail()"/>&nbsp;
		</shiro:hasPermission>	
	</div>
	</c:if>
	
	<form id="listForm" method="post">
	<table id="contentTable" class="table table-striped table-bordered table-condensed">
		<thead>
			<tr class="tableHead">
				<th style="text-align:center;" onclick="toggleCheckBox('checkboxName',this)">
		  			<input type="checkbox" checkboxName="sort" name="table_form_checkbox_all" value="">
		  		</th>
		  		<th style="text-align:center;">序号</th>
				<th>名称</th>
				<th>上级菜单</th>
				<th>链接</th>
				<th>图标</th>
				<th>排序</th>
				<th>可见</th>
				<th>权限标识</th>
				<th>目标</th>
				<th>备注信息</th>
				<th>更新时间</th>
				<shiro:hasPermission name="sys:menu:edit"><th>操作</th></shiro:hasPermission>
			</tr>
		</thead>
		<tbody>
		<c:forEach items="${page.list}" var="menu" varStatus="status">
			<tr>
				<td style="text-align:center;" >
					<input type="checkbox" name="sort" attrname="sort" value="">
					<input type="hidden" attrname="id" value="${menu.id}"/>
					<input type="hidden" attrname="parent.id" value="${menu.parent.id}"/>
					<input type="hidden" attrname="parentIds" value="${menu.parentIds}"/>
					<input type="hidden" attrname="name" value="${menu.name}"/>
					<input type="hidden" attrname="href" value="${menu.href}"/>
					<input type="hidden" attrname="target" value="${menu.target}"/>
					<input type="hidden" attrname="icon" value="${menu.icon}"/>
					<input type="hidden" attrname="isShow" value="${menu.isShow}"/>
					<input type="hidden" attrname="permission" value="${menu.permission}"/>
					<input type="hidden" attrname="remarks" value="${menu.remarks}"/>
					<input type="hidden" attrname="createBy.id" value="${menu.createBy.id}"/>
					<input type="hidden" attrname="createDate" value="${menu.createDate}"/>
					<input type="hidden" attrname="updateBy.id" value="${menu.updateBy.id}"/>
					<input type="hidden" attrname="updateDate" value="${menu.updateDate}"/>
					<input type="hidden" attrname="delFlag" value="${menu.delFlag}"/>
				</td>
				<td style="text-align:center;">
					<span attrname="number">${ status.index + 1}</span>
				</td>
				<td title="${menu.name}"><a href="javascript:void(0);" onclick="detail(this)">
					${menu.name}
				</a></td>
				<td title="${menu.parent.name}">
					${menu.parent.name}
				</td>
				<td title="">
					${menu.href}
				</td>
				<td title="${menu.icon}" style="vertical-align: middle;text-align: center;">
					<i name="iconIcon" class="${menu.icon}"></i>
				</td>
				<td style="vertical-align: middle;text-align: center;">
					<input type="hidden" name="ids" value="${menu.id}"/>
					<input type="text" name="sorts" value="${menu.sort}" maxlength="50" class="required" style="width:50px;height: 14px;margin: 0px;"/>
				</td>
				<td title="是否在菜单中显示" style="vertical-align: middle;text-align: center;">
					${fns:getDictByTypeValue(menu.isShow,'show_hide').label}
				</td>
				<td title="">
					${menu.permission}
				</td>
				<td title="">
					${menu.target}
				</td>
				<td title="">
					${menu.remarks}
				</td>
				<td title="">
					<fmt:formatDate value="${menu.updateDate}" pattern="yyyy-MM-dd HH:mm:ss"/>
				</td>
				<shiro:hasPermission name="sys:menu:edit"><td>
				<c:if test='${menu.refer.type == 0 || empty menu.refer.type }'>
					<a href="javascript:void(0);" onclick="edit(this)">修改</a>
					<a href="javascript:void(0);" onclick="del(this)">删除</a>
				</c:if>
				</td></shiro:hasPermission>
			</tr>
		</c:forEach>
		</tbody>
	</table>
	<div class="pagination">${page}</div>
	</form>
	
	<!-- 关联选择 -->
	<c:if test='${menu.refer.type == 1}'>
		<div style="text-align: right;display: block;padding-right: 30px;margin-top:-20px;">
			<input id="btnOk" class="btn btn-primary" type="reset" value="确 定" onclick="ok()"/>
			<input id="btnCancel" class="btn " type="button" value="取 消" onclick="cancel()"/>
		</div>
		<script type="text/javascript">
		
		$(document).ready(function() {
			jQuery('#contentTable').find('tr').live('dblclick', function(event){ 
				setArtcleValue(this);
			});
		});
		
		function ok(obj){ //取消操作
			var checkboxs = jQuery('#contentTable').find('[attrname="sort"]:checked');
			if (checkboxs.length == 0) {
				alertx("请选择一行数据！");return;
			} else if (checkboxs.length > 1) {
				alertx("只能选择一行数据！");return;
			}
			var curentTr = jQuery(checkboxs[0]).parents('tr')[0];
			setArtcleValue(curentTr);
		}
		
		function setArtcleValue(curentTr){
			var refmap = jQuery('[name="refer.map"]').val();
			var data = getDataByReferMap(refmap,curentTr);
			parent.ok(data);
			parent.closeWindows();
		}
		
		function cancel(){ //取消操作
			parent.closeWindows();
		}
			
		</script>
	</c:if>
	
	
	<div style="height: 50px;width: 100%;"></div>
</body>
</html>